<?php
class cls_control_cart extends cls_control_base
{
	var $id;
	var $num;
	var $product_id;

	function __construct()
	{
		$this->id=$_REQUEST['id'];
		
		$this->num=$_REQUEST['num'];
		
		$this->product_id=$_REQUEST['product_id'];		
	}
	
	function cart_show()
	{
		$product_serve = cls_serve_base::get_instance($this->name);
		$productdetail_serve = cls_serve_base::get_instance('productdetail');
		
		$cart=$_SESSION['cart'];
		for($i=0;!empty($cart[$i]['id']);$i++)
		{
			$product_id=$cart[$i]['id'];
			$product_num=$cart[$i]['num'];
			$discount_info=$productdetail_serve->get_product_discount($product_id);
			$product_info=array(
					"id"=>$i,
					"product_id"=>$product_id,
					"name"=>$product_serve->get_product_name($product_id),
					"image"=>$product_serve->get_product_image($product_id),
					"author"=>$product_serve->get_product_author($product_id),
					"current_price"=>sprintf("%.2f",$productdetail_serve->get_current_price($product_id)),
					"saved_value"=>sprintf("%.2f",$productdetail_serve->get_saved_value($product_id)),
					"saved_proportion"=>sprintf("%.1f",$productdetail_serve->get_saved_proportion($product_id)),
					"discount_type"=>$discount_info["Type"],					
					'num'=>$product_num,
					'quantity'=>$product_serve->get_product_quantity($product_id)				
					);
			$cart_show[$i]=$product_info;
		}

		echo json_encode($cart_show);
		
	}
	
	function cart_sum()
	{
		$cart_sum=0;
		$cart=$_SESSION['cart'];
		for($i=0;!empty($cart[$i]);$i++)
		{
			$cart_sum = $cart_sum + $cart[$i]['num']*$cart[$i]['price'];
		}
		return $cart_sum;
	}
	
	function get_sum()
	{
		$sum=$this->cart_sum();
		echo json_encode($sum);
	}
	
	function cart_add()
	{
		$product_serve = cls_serve_base::get_instance($this->name);
		$product_serve->cart_add($this->product_id,$this->num);
		$x = array('msg'=>"添加成功");
		echo json_encode($x);
	}
	
	function cart_num_add()
	{
		$product_serve = cls_serve_base::get_instance($this->name);
		$product_serve->cart_num_add($this->id);
		//$cart_tmp=$_SESSION['cart'];
		//$num=$cart_tmp[$this->id][1];
		//echo json_encode($num);
	}
	
	function cart_num_minus()
	{
		$product_serve = cls_serve_base::get_instance($this->name);
		$product_serve->cart_num_minus($this->id);
	}
	
	function cart_delete()
	{
		$product_serve = cls_serve_base::get_instance($this->name);
		$product_serve->cart_delete($this->id);
	}
	
	function test_array()
	{
		$product_serve = cls_serve_base::get_instance($this->name);
		$product_serve->cart_add(6,1);
		$product_serve->cart_add(7,2);
		$product_serve->cart_add(9,3);
		$product_serve->cart_add(10,4);
	}
	
/*	function cart_empty()
	{
		$product_serve = cls_serve_base::get_instance($this->name);
		$product_serve->cart_empty();		
	}
*/
	
	function st()
	{
		$_SESSION['count']++;
		p($_SESSION);
	}
	
	function cart_empty()
	{
		unset($_SESSION['cart']);
	}
	
	function sh()
	{
		p($_SESSION);
	}
	
	function test()
	{
		$cart=$_SESSION['cart'];
		p($cart['price']);
	}
}